package com.oscar.ehome.view.pageview;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.oscar.ehome.R;
import com.oscar.ehome.base.C;
import com.oscar.ehome.base.activity.BasesActivity;
import com.oscar.ehome.bean.pagebean.YeJiListBean;
import com.oscar.ehome.presenter.PagePresenter;
import com.oscar.ehome.provider.gildeimage.GildeUtils;
import com.oscar.ehome.utils.system.SystemConfigFactory;
import com.oscar.ehome.utils.util.CommonUtils;
import com.oscar.ehome.utils.util.ToastUtil;
import com.oscar.ehome.utils.view.MyImageView;
import com.oscar.ehome.utils.view.MyList;
import com.oscar.ehome.utils.view.progressview.ProgressFrameLayout;
import com.oscar.ehome.utils.xrefresh.XRefreshView;
import com.oscar.ehome.view.loginview.LoginActivity;
import com.oscar.ehome.view.pageview.adapter.StudyAdapter;
import com.oscar.ehome.view.pageview.adapter.YeJiListAdapter;
import com.oscar.ehome.view.pageview.view.YeJiChaXunView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by juxing
 * author: SunHaoZhao
 * date: On 2018/4/18
 */

public class YeJiChaXunList extends BasesActivity<YeJiChaXunView, PagePresenter> implements YeJiChaXunView, XRefreshView.XRefreshViewListener,
        AdapterView.OnItemClickListener, ProgressFrameLayout.AgainRequestListener {
    PagePresenter pagePresenter;

    public static long lastRefreshTime;
    String token = "";
    @BindView(R.id.title_img_back)
    ImageView titleImgBack;
    @BindView(R.id.title_tv_menu)
    TextView titleTvMenu;
    @BindView(R.id.title_layout_left)
    RelativeLayout titleLayoutLeft;
    @BindView(R.id.title_topbar)
    TextView titleTopbar;
    @BindView(R.id.img_spinner)
    ImageView imgSpinner;
    @BindView(R.id.liear_title)
    LinearLayout liearTitle;
    @BindView(R.id.tuanduiitem)
    LinearLayout tuanduiitem;
    @BindView(R.id.title_tv_right)
    TextView titleTvRight;
    @BindView(R.id.title_img_right)
    ImageView titleImgRight;
    @BindView(R.id.title_img_right_new)
    ImageView titleImgRightNew;
    @BindView(R.id.title_img_right_dudao)
    ImageView titleImgRightDudao;
    @BindView(R.id.home_tv_num)
    TextView homeTvNum;
    @BindView(R.id.title_right_layput)
    RelativeLayout titleRightLayput;
    @BindView(R.id.basetitle_bar)
    RelativeLayout basetitleBar;
    @BindView(R.id.touxiang_img)
    MyImageView touxiangImg;
    @BindView(R.id.name_text)
    TextView nameText;
    @BindView(R.id.xingbie_img)
    ImageView xingbieImg;
    @BindView(R.id.info_lin)
    LinearLayout infoLin;
    @BindView(R.id.xinzengyouxiao_text)
    TextView xinzengyouxiaoText;
    @BindView(R.id.dangyueyushou_text)
    TextView dangyueyushouText;
    @BindView(R.id.nianduyushou_text)
    TextView nianduyushouText;
    @BindView(R.id.yejichaxunlist)
    MyList yejichaxunlist;
    @BindView(R.id.yeji_refview)
    XRefreshView yejiRefview;
    @BindView(R.id.yeji_fragment)
    ProgressFrameLayout yejiFragment;
    private int page = 1;//页数
    private int page_size = 20;//每页的条数
    int size = 0;
    String shijian;
    YeJiListAdapter adapter;
    List<YeJiListBean.yonghu> learnList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yejichaxunlist);
        ButterKnife.bind(this);
        basetitleBar.setBackgroundColor(Color.parseColor(SystemConfigFactory.getInstance(YeJiChaXunList.this).getSystemConfig().getColor()));
        initView();
        initData();
        onClick();
    }

    public void initView() {
        titleImgBack.setImageResource(R.mipmap.fanhui);
        titleImgBack.setVisibility(View.VISIBLE);
        titleTopbar.setText("业绩查询");

        token = SystemConfigFactory.getInstance(YeJiChaXunList.this).getSystemConfig().getToken();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");// HH:mm:ss
//获取当前时间
        Date date = new Date(System.currentTimeMillis());
        shijian = simpleDateFormat.format(date);
        adapter = new YeJiListAdapter(this, learnList);
        yejichaxunlist.setAdapter(adapter);
    }

    /**
     * 点击
     */
    public void onClick() {

        yejichaxunlist.setOnItemClickListener(this);

        yejiFragment.setAgainRequestListener(this);
        yejiRefview.setXRefreshViewListener(this);
        // 设置是否可以下拉刷新
        yejiRefview.setPullRefreshEnable(true);
        // 设置是否可以上拉加载
        yejiRefview.setPullLoadEnable(false);
        // 设置上次刷新的时间
        yejiRefview.restoreLastRefreshTime(lastRefreshTime);
        //当下拉刷新被禁用时，调用这个方法并传入false可以不让头部被下拉
        yejiRefview.setMoveHeadWhenDisablePullRefresh(true);
        // 设置时候可以自动刷新
        yejiRefview.setAutoRefresh(false);
    }

    /**
     * 加载数据
     */
    public void initData() {
        if (isNetWork()) {

            getmessage();
            showLoading();
        } else {
        }
    }

    public void getmessage() {
        pagePresenter.getyejilist(token, "20", "1", shijian);

    }

    public boolean isNetWork() {
        boolean netWork = true;
        if (!CommonUtils.isNetConn(this)) {
            netWork = false;
            return netWork;
        }
        return netWork;
    }

    @OnClick({R.id.title_layout_left,R.id.tuanduiitem})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tuanduiitem:
                Intent in=new Intent();
                in.setClass(YeJiChaXunList.this,YeJiChaXunXiangQing.class);
                in.putExtra("id",learnList.get(0).getUser().getId());
                in.putExtra("data",shijian);
                startActivity(in);
                break;
            case R.id.title_layout_left://返回
                finish();
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Intent in=new Intent();
        in.setClass(YeJiChaXunList.this,YeJiChaXunXiangQing.class);
        in.putExtra("id",learnList.get(i).getUser().getId());
        in.putExtra("data",shijian);
        startActivity(in);
    }

    @Override
    public void getYeLJiList(YeJiListBean result) {
        dismissLoading();
        if (result != null) {
            Log.e("525252", "getstudyList: " + result);
            if (page == 1) {
                learnList.clear(); // 将原有的内容清空
            }
            learnList = result.getList();
            size = learnList.size();
            if (size > 0) {
                nameText.setText(learnList.get(0).getUser().getName());
                GildeUtils.loadUrlImage4(C.qiniu_ip + learnList.get(0).getUser().getPhoto(), touxiangImg, YeJiChaXunList.this);
                if (learnList.get(0).getUser().getSex().equals("男")) {
                    xingbieImg.setImageResource(R.mipmap.nan);
                } else {
                    xingbieImg.setImageResource(R.mipmap.nv);
                }
                xinzengyouxiaoText.setText(learnList.get(0).getValue7()+"人");
                dangyueyushouText.setText(learnList.get(0).getValue2()+"元");
                nianduyushouText.setText(learnList.get(0).getValue4()+"万元");
                yejichaxunlist.setVisibility(View.VISIBLE);
                yejiRefview.stopRefresh();
                yejiFragment.stopRefresh();
                if (size < page_size && learnList.size() > 0) {
                    yejiRefview.stopLoadMore();
                }
                adapter.setLists(learnList, 1);
            } else {
                yejichaxunlist.setVisibility(View.GONE);
                yejiFragment.setVisibility(View.VISIBLE);
                yejiFragment.setNoResult();
            }
        } else {
            yejichaxunlist.setVisibility(View.GONE);
            yejiFragment.setNoResult();
        }


    }

    @Override
    public void erroy() {
        dismissLoading();
        yejiRefview.stopRefresh();
        ToastUtil.show(YeJiChaXunList.this, getString(R.string.erry));
    }

    @Override
    public void erry(String result) {
        dismissLoading();
        yejiRefview.stopRefresh();
        Toast.makeText(YeJiChaXunList.this, result, Toast.LENGTH_SHORT).show();

        yejiFragment.setNoResult();

        if (result.contains("过期")) {
            getSharedPreferences("appbase", Context.MODE_PRIVATE).edit().clear().commit();
            SystemConfigFactory.getInstance(YeJiChaXunList.this).getSystemConfig().SetLogin(false);
            SystemConfigFactory.getInstance(this).getSystemConfig().setIsFirst(true);
            Intent intent = new Intent(YeJiChaXunList.this, LoginActivity.class);
            startActivity(intent);
            finish();
        }
    }

    @Override
    public void againRequest() {
        if (isNetWork()) {
            initData();
        } else {
            ToastUtil.show(YeJiChaXunList.this, R.string.pull_to_refresh_network_error);
        }
    }

    @Override
    protected PagePresenter createPresenter() {
        pagePresenter = new PagePresenter(this);
        return pagePresenter;
    }


    @Override
    public void onRefresh() {

    }

    @Override
    public void onRefresh(boolean isPullDown) {
        lastRefreshTime = yejiRefview.getLastRefreshTime();
        yejiRefview.restoreLastRefreshTime(lastRefreshTime);
        page = 1;
        String s = String.valueOf(page);
        pagePresenter.getyejilist(token, "20", s, shijian);
        showLoading();
    }

    @Override
    public void onLoadMore(boolean isSilence) {
        page++;
        String s = String.valueOf(page);
        pagePresenter.getyejilist(token, "20", s, shijian);
        showLoading();
    }

    @Override
    public void onRelease(float direction) {

    }

    @Override
    public void onHeaderMove(double headerMovePercent, int offsetY) {

    }
}
